package com.example.quartz.service.impl;

import com.example.quartz.dao.QuartzDao;
import com.example.quartz.service.QuartzService;
import com.example.quartz.to.QuartzTo;
import com.example.quartz.vo.QuartzVo;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 杨豹
 * @create 2021/8/12 0012 14:48
 * @description 描述
 */
@Service
public class QuartzServiceImpl implements QuartzService {
    @Autowired
    private QuartzDao quartzDao;
    @Autowired
    private Scheduler scheduler;

    @Override
    public List<QuartzTo> selectQuartzList(QuartzVo quartz) {
        return quartzDao.selectQuartzList(quartz);
    }

//    @Override
//    public int add(QuartzVo vo) {
//        vo.setExchangeCreateReviser("hello");
//        vo.setExchangeReviser("world");
//        int rows = quartzDao.add(vo);
//        if (rows > 0) {
//            createScheduleJob(scheduler, vo);
//        }
//        return rows;
//    }


//    /**
//     * 创建定时任务
//     */
//    public static void createScheduleJob(Scheduler scheduler, QuartzVo job){
//        Class<? extends Job> jobClass = getQuartzJobClass(job);
//        // 构建job信息
//        Long jobId = job.getJobId();
//        String jobGroup = job.getJobGroup();
//        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(getJobKey(jobId, jobGroup)).build();
//
//        // 表达式调度构建器
//        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//        cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);
//
//        // 按新的cronExpression表达式构建一个新的trigger
//        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobId, jobGroup))
//                .withSchedule(cronScheduleBuilder).build();
//
//        // 放入参数，运行时的方法可以获取
//        jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
//
//        // 判断是否存在
//        if (scheduler.checkExists(getJobKey(jobId, jobGroup))) {
//            // 防止创建时存在数据问题 先移除，然后在执行创建操作
//            scheduler.deleteJob(getJobKey(jobId, jobGroup));
//        }
//
//        scheduler.scheduleJob(jobDetail, trigger);
//
//        // 暂停任务
//        if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue())) {
//            scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
//        }
//    }
}
